﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace TierApplication
{
	/// <summary>
	/// 类T_City。
	/// </summary>
	public class T_City
	{
		public T_City()
		{}
		#region Model
		private string _cityid;
		private string _cityname;
		private string _region;
		private string _pcityid;
		/// <summary>
		/// 
		/// </summary>
		public string CityID
		{
			set{ _cityid=value;}
			get{return _cityid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string CityName
		{
			set{ _cityname=value;}
			get{return _cityname;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string Region
		{
			set{ _region=value;}
			get{return _region;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string PCityID
		{
			set{ _pcityid=value;}
			get{return _pcityid;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public T_City(string CityID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append("CityID,CityName,Region,PCityID ");
			strSql.Append(" from T_City ");
			strSql.Append(" where CityID='"+CityID+"' ");
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				CityID=ds.Tables[0].Rows[0]["CityID"].ToString();
				CityName=ds.Tables[0].Rows[0]["CityName"].ToString();
				Region=ds.Tables[0].Rows[0]["Region"].ToString();
				PCityID=ds.Tables[0].Rows[0]["PCityID"].ToString();
			}
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string CityID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_City where CityID='"+CityID+"' " );
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [T_City](");
			strSql.Append("CityID,CityName,Region,PCityID");
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append("'"+CityID+"',");
			strSql.Append("'"+CityName+"',");
			strSql.Append("'"+Region+"',");
			strSql.Append("'"+PCityID+"'");
			strSql.Append(")");
			DbHelperSQL.ExecuteSql(strSql.ToString());
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_City set ");
			strSql.Append("CityName='"+CityName+"',");
			strSql.Append("Region='"+Region+"',");
			strSql.Append("PCityID='"+PCityID+"'");
			strSql.Append(" where CityID='"+CityID+"' ");
			DbHelperSQL.ExecuteSql(strSql.ToString());
		}


		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(string CityID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_City ");
			strSql.Append(" where CityID='"+CityID+"' ");
				DbHelperSQL.ExecuteSql(strSql.ToString());
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(string CityID)
		{

			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" CityID,CityName,Region,PCityID ");
			strSql.Append(" from T_City ");
			strSql.Append(" where CityID='"+CityID+"' " );
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				CityID=ds.Tables[0].Rows[0]["CityID"].ToString();
				CityName=ds.Tables[0].Rows[0]["CityName"].ToString();
				Region=ds.Tables[0].Rows[0]["Region"].ToString();
				PCityID=ds.Tables[0].Rows[0]["PCityID"].ToString();
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM T_City ");
			if(strWhere.Trim()!="")
			{

				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

        public static DataSet GetAllCityList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_City ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        public static DataSet GetCityListByRegion(string sRegion)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_City ");
            strSql.Append(" where 1=1 ");
            if (sRegion != "")
            {
                strSql.Append(" and Region = " + sRegion);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  Method
	}
}

